package com.ruoyi.web.controller.wx.request;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.security.cert.X509Certificate;

import static org.apache.http.entity.ContentType.APPLICATION_JSON;

/**
 * 微信支付专用类 请求操作方法
 *
 * @author kaur
 */
@Slf4j
public class WxHttpUtil {

    @SneakyThrows
    public static String getTransQueryRequest(String requestUrl, String paySerialNo, String mchId, String privateKeyPath, String url) {
        HttpGet httpPost = createHttpGet(requestUrl, paySerialNo, mchId, privateKeyPath, url);
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = httpClient.execute(httpPost);
        log.info("response:{}", response);
        HttpEntity entity = response.getEntity();
        return EntityUtils.toString(entity);
    }

    private static HttpGet createHttpGet(String requestUrl, String paySerialNo, String mchId, String privateKeyPath, String servletPath) {
        HttpGet httpGet = new HttpGet(requestUrl);
        // NOTE: 建议指定charset=utf-8。低于4.4.6版本的HttpCore，不能正确的设置字符集，可能导致签名错误
        httpGet.addHeader("Content-Type", "application/json");
        httpGet.addHeader("Accept", "application/json");
        httpGet.addHeader("Wechatpay-Serial", paySerialNo);
        String strToken = null;
        try {
            strToken = WxPayV3Util.getToken("GET", servletPath, "", mchId, paySerialNo, privateKeyPath);
        } catch (Exception e) {
            log.error("createHttpGet error:", e);
            e.printStackTrace();
        }
        log.info("token " + strToken);
        httpGet.addHeader("Authorization", "WECHATPAY2-SHA256-RSA2048" + " " + strToken);
        return httpGet;
    }

    public static HttpGet createHttpGetTest(String requestUrl, String paySerialNo, String mchId, String privateKeyPath, String servletPath) {
        HttpGet httpGet = new HttpGet(requestUrl);
        httpGet.addHeader("Accept", "application/json");
        httpGet.addHeader("Wechatpay-Serial", paySerialNo);
        httpGet.addHeader("User-Agent", "用户代理(https://zh.wikipedia.org/wiki/User_agent)");
        String strToken = null;
        try {
            strToken = WxPayV3Util.getToken("GET", servletPath, "", mchId, paySerialNo, privateKeyPath);
        } catch (Exception e) {
            log.error("createHttpGet error:", e);
            e.printStackTrace();
        }
        log.info("token " + strToken);
        httpGet.addHeader("Authorization", "WECHATPAY2-SHA256-RSA2048" + " " + strToken);
        return httpGet;
    }

    @SneakyThrows
    public static HttpPost httpPost(String requestUrl, String requestJson, String paySerialNo, String mchId, String privateKeyPath, String servletPath) {
        HttpPost httpPost = new HttpPost(requestUrl);
        httpPost.addHeader("Accept", "application/json");
        httpPost.addHeader("Content-Type", "application/json");
        String pub = "D:\\DeskTop\\haojiuxuan\\admin\\src\\main\\resources\\public_key.pem";
        X509Certificate x509Certificate = WxPayV3Util.getCertificate(pub);
        String sn = x509Certificate.getSerialNumber().toString(16);
        httpPost.addHeader("Wechatpay-Serial", sn.toUpperCase());
        String strToken = null;
        try {
            log.info("requestJson:{}", requestJson);
            strToken = WxPayV3Util.getToken("POST", servletPath, requestJson, mchId, paySerialNo, privateKeyPath);
        } catch (Exception e) {
            log.error("createHttpPost error:", e);
            e.printStackTrace();
        }
        StringEntity reqEntity = new StringEntity(requestJson, APPLICATION_JSON);
        log.info("token " + strToken);
        httpPost.addHeader("Authorization", "WECHATPAY2-SHA256-RSA2048" + " " + strToken);
        httpPost.setEntity(reqEntity);
        return httpPost;
    }

    @SneakyThrows
    public static HttpPost httpPostToTransfer(String requestUrl, String requestJson, String paySerialNo, String mchId, String privateKeyPath, String servletPath) {
        HttpPost httpPost = new HttpPost(requestUrl);
        httpPost.addHeader("Accept", "application/json");
        httpPost.addHeader("Content-Type", "application/json");
        httpPost.addHeader("Wechatpay-Serial", "1BA28D929D355633FDDA0EE43B72E1A92DD3B524");
        String strToken = null;
        try {
            log.info("requestJson:{}", requestJson);
            strToken = WxPayV3Util.getToken("POST", servletPath, requestJson, mchId, paySerialNo, privateKeyPath);
        } catch (Exception e) {
            log.error("createHttpPost error:", e);
            e.printStackTrace();
        }
        StringEntity reqEntity = new StringEntity(requestJson, APPLICATION_JSON);
        log.info("token " + strToken);
        httpPost.addHeader("Authorization", "WECHATPAY2-SHA256-RSA2048" + " " + strToken);
        httpPost.setEntity(reqEntity);
        return httpPost;
    }
}
